//
//  GRViewController.m
//  GRFMRemotePlayer
//
//  Created by guorui on 01/15/2018.
//  Copyright (c) 2018 guorui. All rights reserved.
//

#import "GRViewController.h"
#import "GRFMRemotePlayer.h"

@interface GRViewController ()

@property (strong, nonatomic) GRFMRemotePlayer *player;
@property (weak, nonatomic) IBOutlet UISlider *playSlider;
@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *loadSlider;

@property (weak, nonatomic) NSTimer *timer;

@end

@implementation GRViewController

- (NSTimer *)timer {
    if (!_timer) {
        NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        _timer = timer;
    }
    return _timer;
}
    
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.player = [[GRFMRemotePlayer alloc] init];
    [self timer];
}

#pragma mark - Actions
- (void)update
{
    self.currentTimeLabel.text = [self.player currentTimeFormat];
    self.totalTimeLabel.text = [self.player totalTimeFormat];
    self.playSlider.value = self.player.progress;
    self.loadSlider.progress = self.player.loadDataProgress;
}

- (IBAction)play:(id)sender {
    // http://audio.xmcdn.com/group23/M04/63/C5/wKgJNFg2qdLCziiYAGQxcTOSBEw402.m4a
    [self.player playWithURL:[NSURL URLWithString:@"http://mp4.22mtv.com:9090/mp43/20925-MichaelLearnsToRock-TakeMeToYourHeart[68mtv.com].mp4"] isCache:YES];
}

- (IBAction)pause:(id)sender {
    [self.player pause];
}

- (IBAction)resume:(id)sender {
    [self.player resume];
}

- (IBAction)kuaijin:(id)sender {
    [self.player seekToTimeDiffer:10];
}

- (IBAction)progressChange:(UISlider *)sender {
    [self.player seekToProgress:sender.value];
}

- (IBAction)rate:(id)sender {
    [self.player setRate:2];
}
- (IBAction)muted:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self.player setMuted:sender.selected];
}
- (IBAction)volumChange:(UISlider *)sender {
    [self.player setVolume:sender.value];
}
@end
